<?php

namespace App\Models;

use App\Models\Traits\Btn;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Base extends Model
{
    //软删除
    use SoftDeletes,Btn;
    //设置软删除所标记的字段
    protected $dates = ['deleted_at'];

    //设置添加时候的黑名单
    protected $guarded = [];

    /**
     * 数组的合并,并加上html标志前缀
     * @param array $data
     * @param int $pid
     * @param string $html
     * @param int $level
     * @return array
     * 
    */
    public function treeLevel(array $data,int $pid = 0,string $html = '--',int $level = 0){
        static $arr = [];
        foreach($data as $val){
            //代表是顶级菜单
            if($val['pid'] == $pid){
                //重复一个字符多少次
                $val['html'] = str_repeat($html,$level*2);
                $val['level'] = $level + 1;
                $arr[] = $val;
                $this->treeLevel($data,$val['id'],$html,$val['level']);
            }
        }
        return $arr;
    }
	
	
	//数据多层级
	public function subTree(array $data,int $pid = 0){
		//返回的结果
		$arr = [];
		foreach($data as $val){
			//给定的pid是当前记录的上级id
			if($pid == $val['pid']){
				//进行递归
				$val['sub'] = $this->subTree($data,$val['id']);
				$arr[] = $val;
			}
		}
		return $arr;
	}
}
